<template>
    <div class="create_room_box" >
        <div class="list">
            <div class="list_item" v-for="(item,index) in users" :key="index">
                <div id="head_image">
                    <img style="height: 100%;width: 100%;border-radius: 50%;" 
                    :src="item?.friend_info?.head_image || '/public/yonghu.png' " alt="">
                </div>
                <div id="meta">
                    <div>
                        {{item?.friend_info?.username || item?.friend_info?.id }}
                    </div>
                    <div>
                        {{item?.friend_info?.introduction || '这个人很懒,什么也没有留下' }}   
                    </div>
                </div>
                <div  @click="select_click(item)"  :class="{
                    'select_item':true,
                    'create_group_isselect': item.isselect
                }"
                >
                <div style="width: 100%;height: 100%;padding: 5px;border: 5px solid white;border-radius: 50%;"></div>
                </div>
            </div>
        </div>
        <div class="func">
            <div class="func_item" v-for="(i,index) in func_items" :key="i.value"
            @click.stop="i.onclick">
               {{i.name}}
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, inject, computed, watch } from "vue"
import { chat_createRoom ,get_user_GroupList} from "@/api/chat/room.js"
const chat = inject('chat')()
const select_ids = ref({})
const users = ref([])
const middle_data = computed(() => { 
    const middle_data = chat.value.get_friend_list() || []
        middle_data.forEach(element => {
            element['isselect']=false
        });
    return middle_data
})
watch(() => middle_data.value, (newValue) => { 
    users.value=newValue
}, { immediate: true })
const select_click=(item) =>{ 
    item.isselect = !item.isselect
    if (item.isselect) {
        select_ids.value[item.friend_info.id] = true
    } else { 
        delete  select_ids.value[item?.friend_info?.id]
    }
}

const func_items = [
    {
        'name': '创建群组',
        'icon': 'el-icon-plus',
        'value': 'create_group',
        'onclick': async () => {
            let ids = []
            ids.push(chat.value?.get_user_info()?.id)
            Object.keys(select_ids.value).forEach((key) => {
                if (select_ids.value[key]){ids.push(key) }
            });
            const judge = await chat_createRoom(ids,chat.value?.get_user_info()?.id,'group')
            if (judge == 200) { 
                const new_groups= await get_user_GroupList(chat.value?.get_user_info()?.id)
                if (new_groups !== undefined) { 
                    chat.value.set_group_list(new_groups)
                }
            }
        }
    },
    {
        'name': '清空选择',
        'icon': 'el-icon-plus',
        'value': 'clear_select',
        'onclick': () => {
            select_ids.value = {}
            users.value.forEach(element => {
                element.isselect =false 
            });
    }
    }, {
        'name': '反选',
        'icon': 'el-icon-plus',
        'value': 'reverse_select',
        'onclick': () => {
            users.value.forEach(element => {
                if (select_ids.value[element?.friend_info?.id]) { 
                    delete select_ids.value[element?.friend_info?.id]
                } else { 
                    select_ids.value[element?.friend_info?.id] = true
                }
                element.isselect = !element.isselect
            });
        }
    }, {
        'name': '全选',
        'icon': 'el-icon-plus',
        'value': 'all_select',
        'onclick': () => {
            users.value.forEach(element => {
                element.isselect = true
                select_ids.value[element?.friend_info?.id]=true
            });
        }
    }
]






</script>

<style scoped>
.create_room_box{
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: row;
    gap:10px;
    user-select: none;
    background-color:#a7e9c8;
    border-radius: 8px;
  
}
.list{
    width: 70%;
    overflow: scroll;   
    border-radius: 8px 0 0 8px ;
}
.list::-webkit-scrollbar{
    display: none;
}
.func{
   width: 30%;
   /* background-color: #a0c2fc; */
   border-radius: 0 8px 8px 0;
   display: flex;
   flex-direction: column;
   justify-self: center;
   gap:10px
}
.list_item{
    background-color:#f8f8f9;
    display: flex;
    width: 100%;
    position: relative;
    margin-bottom: 5px;
    border-radius: 5px 25px 25px 5px;
}
#head_image{
    width: 35px;
    height: 35px;
}
#meta{
    height: 35px;
    display: flex;
    flex-direction: column;
}
#meta>div:nth-child(1){
    height: 50%;
    font-size: 14px;
    width: 150px;
    padding-left: 5px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
#meta>div:nth-child(2){
    height: 50%;
    font-size: 12px;
    width: 150px;
    padding-left: 8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.select_item{
    width: 35px;
    height: 100%;
    position: absolute;
    right: 0px;
    line-height: 35px;
    margin: 0 auto;
    background-color: #c5c8ce;
    border-radius: 50%;
}
.create_group_isselect{
    background-color: #19be6b;
    border-radius: 50%;
}
.func_item{
    width: 100%;
    height: 40px;
    text-align: center;
    background-color: #a0c2fc; 
    line-height: 40px;
    border-radius: 5px;
    color:white;
}
.func_item:hover{
    background-color: #a0c2fc; 
    cursor: pointer;
    color: #5cadff
}
.func_item:active{
    background-color: #a0c2fc; 
    cursor: pointer;
    outline: none;
    border: none;
}

</style>